package com.ailk.kbi.kbirazor.util

import groovy.util.XmlSlurper
import groovy.util.logging.Slf4j
import groovy.util.slurpersupport.NodeChild;

import com.ailk.kbi.kbirazor.vo.DBTypeVO
import com.ailk.kbi.kbirazor.vo.DataSourceVO
import com.ailk.kbi.kbirazor.vo.SettingVO


@Slf4j
class SettingUtil {
	public static final String SETTING_FILE = "setting.xml";

	private static SettingVO settingVO

	public static SettingVO getSettingVO(){
		if(settingVO == null){
			SettingUtil settingUtil = new SettingUtil()
			settingUtil.loadSetting()
		}
		settingVO
	}

	private SettingUtil(){

	}

	private void loadSetting(){
		settingVO = new SettingVO()
		def settingXML = new XmlSlurper().parse(Thread.currentThread().getContextClassLoader().getResource(SETTING_FILE).getFile())
		log.info "初始化系统配置..."
		String subsource = settingXML.@subsource
		log.info "子配置文件 = " + subsource
		/*得到运行模式*/
		settingVO.mode = settingXML.@mode
		log.info "运行模式 = " + settingVO.mode

		/*得到运行模式*/
		settingVO.debug = settingXML.@debug == 'true'
		log.info "显示debug内容 = " + settingVO.debug

		settingVO.debug_xml = settingXML.@debug_xml == 'true'
		log.info "显示XML内容 = " + settingVO.debug_xml


		/*得到数字类型*/
		settingVO.numbers = settingXML.datatype.number.text().split(",")
		log.info "数字类型 = " + settingVO.numbers

		/*得到日期类型*/
		settingVO.dates = settingXML.datatype.date.text().split(",")
		log.info "日期类型 = " + settingVO.dates

		/*数据类型*/
		settingVO.dbtypes = []

//		settingXML.dbtypes.dbtype.each { dbtypeXML->
//			NodeChild a
//			a.toString()
//			log.info(dbtypeXML.@typeName.toText())
//			DBTypeVO dbTypeVO = new DBTypeVO(/*typeName:dbtypeXML.@typeName,driverClassName:dbtypeXML.driverClassName,dbuClass:dbtypeXML.dbuClass*/)
//			settingVO.dbtypes.add(dbTypeVO)
//			log.info ">> 数据库驱动 = " + dbTypeVO.driverClassName
//		}



		/*读取SubSetting*/
		def subsettingXML = new XmlSlurper().parse(Thread.currentThread().getContextClassLoader().getResource(subsource).getFile())

		settingVO.subkey = subsettingXML.@subkey
		settingVO.buClass = subsettingXML.@buClass
		log.info "BU实现类 = " + settingVO.buClass

		settingVO.kbsControlImpl = subsettingXML.@kbsControlImpl
		log.info "kbsControl实现类 = " + settingVO.kbsControlImpl

		/*得到模板文件的根目录*/
		//		settingVO.filesRoot = subsettingXML.files.root.find{ it.@mode.text().indexOf(settingVO.mode) > -1 || it.@mode.text() == ''}
		settingVO.filesRoot = subsettingXML.files.root
		log.info "模板文件的根目录 = " + settingVO.filesRoot
		/*存放KBiSheet数据的数据源*/
		settingVO.kbrbase = subsettingXML.datasources.@kbrbase
		log.info "存放KBiSheet数据的数据源 = " + settingVO.kbrbase
		/*数据源*/
		settingVO.datasources = []
		subsettingXML.datasources.datasource.each { datasourceXML->
			//			if(datasourceXML.@mode.text() == '' || datasourceXML.@mode.text().indexOf(settingVO.mode) > -1){
			//				DataSourceVO dataSourceVO = new DataSourceVO(typeName:datasourceXML.@typeName,key:datasourceXML.@key,url:datasourceXML.url,user:datasourceXML.user,password:datasourceXML.password,usepool:(datasourceXML.@usepool == "true"))
			//				dataSourceVO.properties = new Properties()
			//				datasourceXML.properties.property.each { propertyXML->
			//					dataSourceVO.properties.setProperty(propertyXML.@key.text(), propertyXML.@value.text())
			//				}
			//				settingVO.datasources.add(dataSourceVO)
			//				log.info ">> 数据源 = " + dataSourceVO.url + "; usepool = " + dataSourceVO.usepool + "; properties = " + dataSourceVO.properties.size() + dataSourceVO.properties
			//			}
		}

		/*得到维表列*/
		settingVO.dimColumns = subsettingXML.dimColumns.text().split(",")
		log.info "维表列 = " + settingVO.dimColumns

		log.info "初始化系统配置完成!"
	}




}
